﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8" />
    <title>Ping: src/main/java/com/stateforge/statemachine/examples/ping/Ping.java Source File</title>
    <link href="/Content/Help.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/tabs.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="header">
        <img id="logo" alt="StateForge" src="/Content/Images/StateForgeLogo.png" />
        <div id="title">
            <h1>
                Examples using state machines generated by StateBuilderJava
            </h1>
            <h3>
                Ping: src/main/java/com/stateforge/statemachine/examples/ping/Ping.java Source File
            </h3>
        </div>
        <div id="menucontainer">
            <ul id="menu">
                <li><a href="/">Home</a></li>
                <li><a href="/Home/Download">Downloads</a></li>
                <li><a href="/Help/state-machine-tools.aspx">Help</a></li>
                <li><a href="/Home/Faq">F.A.Q</a></li>
                <li><a href="/Home/About">Contact us</a></li>
            </ul>
        </div>
    </div>
<!-- Generated by Doxygen 1.7.4 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Packages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">src/main/java/com/stateforge/statemachine/examples/ping/Ping.java</div>  </div>
</div>
<div class="contents">
<a href="_ping_8java.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="keyword">package </span>com.stateforge.statemachine.examples.ping;
<a name="l00002"></a>00002 
<a name="l00003"></a>00003 <span class="keyword">import</span> java.util.logging.Level;
<a name="l00004"></a>00004 <span class="keyword">import</span> java.util.logging.Logger;
<a name="l00005"></a>00005 
<a name="l00006"></a>00006 <span class="keyword">import</span> com.stateforge.statemachine.context.IContextEnd;
<a name="l00007"></a>00007 <span class="keyword">import</span> com.stateforge.statemachine.listener.ObserverConsole;
<a name="l00008"></a>00008 
<a name="l00013"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html">00013</a> <span class="keyword">public</span> <span class="keyword">class </span><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html">Ping</a>
<a name="l00014"></a>00014 {
<a name="l00015"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#af56354b7b8c942d66b5a0f17252c0e91">00015</a>     <span class="keyword">private</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html">EchoClient</a> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#af56354b7b8c942d66b5a0f17252c0e91">echoClient</a>;
<a name="l00016"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a241ba63cfe6f227a670dd658a2efe345">00016</a>     <span class="keyword">private</span> <span class="keyword">final</span> PingContext <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a241ba63cfe6f227a670dd658a2efe345">context</a>;
<a name="l00017"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a3663cc57d600f444c8890aadf539d7ce">00017</a>     <span class="keyword">private</span> <span class="keywordtype">int</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a3663cc57d600f444c8890aadf539d7ce">rx</a>; <span class="comment">// Number of packet received</span>
<a name="l00018"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a9671e1e8f3d8844ec5ba49c20602fedf">00018</a>     <span class="keyword">private</span> <span class="keywordtype">int</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a9671e1e8f3d8844ec5ba49c20602fedf">tx</a>; <span class="comment">// Number of packet already sent</span>
<a name="l00019"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a6fe2bc15c87cda622ee2997b7750757f">00019</a>     <span class="keyword">private</span> <span class="keywordtype">int</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a6fe2bc15c87cda622ee2997b7750757f">count</a> = 5; <span class="comment">//Total number of packet to send</span>
<a name="l00020"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#aa066bc6d8f7649c28e3a86e6bcb5a11d">00020</a>     <span class="keyword">private</span> <span class="keywordtype">long</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#aa066bc6d8f7649c28e3a86e6bcb5a11d">timeout</a> = 1000;<span class="comment">// Timeout in msec between 2 packets</span>
<a name="l00021"></a>00021     
<a name="l00022"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a1aa39c43bf959eb6bb938adf99649112">00022</a>     <span class="keyword">private</span> <span class="keyword">final</span> <a class="code" href="interfacecom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_i_ping_listener.html">IPingListener</a> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a1aa39c43bf959eb6bb938adf99649112">listener</a>;
<a name="l00023"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a00abd306f3ef80fa11e7ca1bc5de492e">00023</a>     <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a00abd306f3ef80fa11e7ca1bc5de492e">logger</a> = Logger.getLogger(<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html">Ping</a>.class.getName());
<a name="l00024"></a>00024     
<a name="l00025"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a251b24caadc441d7bc668f9de756f2ad">00025</a>     <span class="keyword">public</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a251b24caadc441d7bc668f9de756f2ad">Ping</a>(<span class="keyword">final</span> <a class="code" href="interfacecom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_i_ping_listener.html">IPingListener</a> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a1aa39c43bf959eb6bb938adf99649112">listener</a>) {
<a name="l00026"></a>00026         this.listener = <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a1aa39c43bf959eb6bb938adf99649112">listener</a>;
<a name="l00027"></a>00027         this.<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a241ba63cfe6f227a670dd658a2efe345">context</a> = <span class="keyword">new</span> PingContext(<span class="keyword">this</span>);
<a name="l00028"></a>00028         this.<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a241ba63cfe6f227a670dd658a2efe345">context</a>.setObserver(ObserverConsole.getInstance());
<a name="l00029"></a>00029         this.<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a241ba63cfe6f227a670dd658a2efe345">context</a>.setEndHandler(<span class="keyword">new</span> IContextEnd() {
<a name="l00030"></a>00030             <span class="keyword">public</span> <span class="keywordtype">void</span> end() {
<a name="l00031"></a>00031                 <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#ac0899a56518a4090289cd022b8870de9">printStatistics</a>();
<a name="l00032"></a>00032                 listener.<a class="code" href="interfacecom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_i_ping_listener.html#a482a48cafd66b6cae2afe132868a103c">end</a>(); 
<a name="l00033"></a>00033             }
<a name="l00034"></a>00034         });
<a name="l00035"></a>00035         
<a name="l00036"></a>00036         this.<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#af56354b7b8c942d66b5a0f17252c0e91">echoClient</a> = <span class="keyword">new</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html">EchoClient</a>(<span class="keyword">new</span> <a class="code" href="interfacecom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_i_echo_client_listener.html">IEchoClientListener</a>() {
<a name="l00037"></a>00037             <span class="keyword">public</span> <span class="keywordtype">void</span> messageReceived() {
<a name="l00038"></a>00038                <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a3663cc57d600f444c8890aadf539d7ce">rx</a>++;
<a name="l00039"></a>00039                <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a241ba63cfe6f227a670dd658a2efe345">context</a>.evPingReply();
<a name="l00040"></a>00040             }
<a name="l00041"></a>00041 
<a name="l00042"></a>00042             <span class="keyword">public</span> <span class="keywordtype">void</span> error(String message) {
<a name="l00043"></a>00043                 <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a241ba63cfe6f227a670dd658a2efe345">context</a>.evError();
<a name="l00044"></a>00044             }
<a name="l00045"></a>00045         });
<a name="l00046"></a>00046     }
<a name="l00047"></a>00047     
<a name="l00048"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a1d42d3706befbf16c5bb374a677d08a9">00048</a>     <span class="keyword">public</span> <span class="keywordtype">int</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a1d42d3706befbf16c5bb374a677d08a9">getRx</a>() {
<a name="l00049"></a>00049         <span class="keywordflow">return</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a3663cc57d600f444c8890aadf539d7ce">rx</a>;
<a name="l00050"></a>00050     }
<a name="l00051"></a>00051 
<a name="l00052"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#aa6e6f3b50e02ba2131c2d46eeff8ca77">00052</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#aa6e6f3b50e02ba2131c2d46eeff8ca77">setRx</a>(<span class="keywordtype">int</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a3663cc57d600f444c8890aadf539d7ce">rx</a>) {
<a name="l00053"></a>00053         this.rx = <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a3663cc57d600f444c8890aadf539d7ce">rx</a>;
<a name="l00054"></a>00054     }
<a name="l00055"></a>00055     
<a name="l00056"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a3d411127043d4fb5f84dd508cffc6192">00056</a>     <span class="keyword">public</span> <span class="keywordtype">int</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a3d411127043d4fb5f84dd508cffc6192">getTx</a>() {
<a name="l00057"></a>00057         <span class="keywordflow">return</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a9671e1e8f3d8844ec5ba49c20602fedf">tx</a>;
<a name="l00058"></a>00058     }
<a name="l00059"></a>00059 
<a name="l00060"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a664f776e73fb8418e4b39b9f6f211734">00060</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a664f776e73fb8418e4b39b9f6f211734">setTx</a>(<span class="keywordtype">int</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a9671e1e8f3d8844ec5ba49c20602fedf">tx</a>) {
<a name="l00061"></a>00061         this.tx = <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a9671e1e8f3d8844ec5ba49c20602fedf">tx</a>;
<a name="l00062"></a>00062     }
<a name="l00063"></a>00063 
<a name="l00064"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a5c370c554efb0f933a7362a8cbff5319">00064</a>     <span class="keyword">public</span> <span class="keywordtype">int</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a5c370c554efb0f933a7362a8cbff5319">getCount</a>() {
<a name="l00065"></a>00065         <span class="keywordflow">return</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a6fe2bc15c87cda622ee2997b7750757f">count</a>;
<a name="l00066"></a>00066     }
<a name="l00067"></a>00067 
<a name="l00068"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a3b929c12421e3421c968fbb2dc730c0f">00068</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a3b929c12421e3421c968fbb2dc730c0f">setCount</a>(<span class="keywordtype">int</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a6fe2bc15c87cda622ee2997b7750757f">count</a>) {
<a name="l00069"></a>00069         this.count = <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a6fe2bc15c87cda622ee2997b7750757f">count</a>;
<a name="l00070"></a>00070     }
<a name="l00071"></a>00071 
<a name="l00072"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a15fc08493b6000c375041660f6cbb858">00072</a>     <span class="keyword">public</span> <span class="keywordtype">long</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a15fc08493b6000c375041660f6cbb858">getTimeout</a>() {
<a name="l00073"></a>00073         <span class="keywordflow">return</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#aa066bc6d8f7649c28e3a86e6bcb5a11d">timeout</a>;
<a name="l00074"></a>00074     }
<a name="l00075"></a>00075 
<a name="l00076"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#aae8b319bed35cc56f584119ef93d5422">00076</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#aae8b319bed35cc56f584119ef93d5422">setTimeout</a>(<span class="keywordtype">long</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#aa066bc6d8f7649c28e3a86e6bcb5a11d">timeout</a>) {
<a name="l00077"></a>00077         this.timeout = <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#aa066bc6d8f7649c28e3a86e6bcb5a11d">timeout</a>;
<a name="l00078"></a>00078     }
<a name="l00079"></a>00079 
<a name="l00080"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a119882e2142830ea9ca3ed3fe035d8a7">00080</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a119882e2142830ea9ca3ed3fe035d8a7">send</a>(){
<a name="l00081"></a>00081         this.<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a9671e1e8f3d8844ec5ba49c20602fedf">tx</a>++;
<a name="l00082"></a>00082         this.<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#af56354b7b8c942d66b5a0f17252c0e91">echoClient</a>.<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ada108fd025e97facfe93d4c91f4e9fa0">send</a>();
<a name="l00083"></a>00083     }
<a name="l00084"></a>00084     
<a name="l00085"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a2f4d03d331d0fc751ebac432fb2cb0a9">00085</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a2f4d03d331d0fc751ebac432fb2cb0a9">start</a>(){
<a name="l00086"></a>00086         this.<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a241ba63cfe6f227a670dd658a2efe345">context</a>.evStart();
<a name="l00087"></a>00087     } 
<a name="l00088"></a>00088     
<a name="l00089"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#ac0899a56518a4090289cd022b8870de9">00089</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#ac0899a56518a4090289cd022b8870de9">printStatistics</a>(){
<a name="l00090"></a>00090         <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_ping.html#a00abd306f3ef80fa11e7ca1bc5de492e">logger</a>.log(Level.INFO, <span class="keyword">this</span>.tx + <span class="stringliteral">&quot; packet(s) sent out of &quot;</span> + <span class="keyword">this</span>.count + <span class="stringliteral">&quot;, lost &quot;</span> + (<span class="keyword">this</span>.tx - <span class="keyword">this</span>.rx));
<a name="l00091"></a>00091     }
<a name="l00092"></a>00092 }
</pre></div></div>
</div>
﻿<div id="footer">
    <hr />
    <p>
        Copyright &copy; 2011 StateForge.</p>
    <hr />
</div>
</body> </html> 
